<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
require APPPATH . "third_party/MX/Controller.php";

class MY_Controller extends MX_Controller {

    /**
     * Holding class instance
     * @var MY_Controller 
     */
    private static $instance = null;

    /**
     * Holding view variables
     * @var array
     */
    public $view_vars = array();

    /**
     * Default layout name
     */
    public $layout_name = 'default';

    /**
     * Disable layout
     * @var boolean
     */
    public $disable_layout = false;

    /**
     * Disable view
     * @var type 
     */
    public $disable_view = false;
    
    /**
     * layout title
     * @var type 
     */
    public $layout_title = 'Dashboard';
    
    /**
     * Stylesheet
     * @var type 
     */
    public $styleSheet = '';
    
    /**
     * headScript
     * @var type 
     */
    public $headScript ='';

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        
        $module_name = $this->router->fetch_module();
        $controller_name = $this->router->fetch_class();
        $method_name = $this->router->fetch_method();
        
        //Load layout config for module
        $this->config->load('layouts', true);

        ($layout_name = $this->config->item($module_name . '/' . $controller_name . '/' . $method_name, 'layouts')) ||
        ($layout_name = $this->config->item($module_name . '/' . $controller_name, 'layouts')) ||
        ($layout_name = $this->config->item($module_name, 'layouts'));

        //If layout is defined
        if ($layout_name)
        {
            $this->layout_name = $layout_name;
        }
        
        //Render menu
        if ($module_name == 'frontend') {
            $this->load->model('category_m');
            
            $arrCategory = $this->category_m->get_tree(array('status' => 1));
            
            $this->view_vars['arrCategory'] = $arrCategory;
        }

        //Assign logged in user info to all views
        $this->load->vars(array(
            '_globals' => array(
                //'user' => $this->auth->get_user(),
                'module_name' => $module_name,
                'controller_name' => $controller_name,
                'method_name' => $method_name
            )
        ));
        
        //Init self instance
        self::$instance = & $this;
    }

    /**
     * Return self instance
     * @return object 
     */
    public static final function get_instance()
    {
        return self::$instance;
    }

    /**
     * Assign value to view
     * @param string $key
     * @param mix $value 
     */
    protected function assign($key, $value)
    {
        $this->view_vars[$key] = $value;
    }

    /**
     * Disable layout
     * @param boolean $disable 
     */
    protected function disable_layout($disable = true)
    {
        $this->disable_layout = $disable;
    }

    /**
     * Disable view
     * @param boolean $disable 
     */
    protected function disable_view($disable = true)
    {
        $this->disable_view = $disable;
    }

    /**
     * Set layout
     * @param string $layout_name 
     */
    public function set_layout($layout_name)
    {
        $this->layout_name = $layout_name;
    }
    
    /**
     * remove Vietnamese unicode
     * @param type $string
     * @return type 
     */
    public function removeUnicode($string){
        $vieUnicode = array ('à' => 'a', 'á' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẫ' => 'a', 'ẩ' => 'a', 'ậ' => 'a', 'ú' => 'a', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'à' => 'a', 'á' => 'a', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ê' => 'e', 'ế' => 'e', 'ề' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ơ' => 'o', 'ớ' => 'o', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'đ' => 'd', 'À' => 'A', 'Á' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'À' => 'A', 'Ẫ' => 'A', 'Ẩ' => 'A', 'Ậ' => 'A', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ô' => 'O', 'Ố' => 'O', 'Ồ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ê' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ư' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Đ' => 'D', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', 'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a', 'ú' => 'u', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'ư' => 'u', 'ứ' => 'u', 'ừ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i', 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'ô', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ơ' => 'o', 'ớ' => 'o', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o', 'đ' => 'd', 'Đ' => 'D', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y', 'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A', 'É' => 'E', 'È' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ẹ' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E', 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ư' => 'U', 'Ứ' => 'U', 'Ừ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U', 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'Ó' => 'O', 'Ò' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ọ' => 'O', 'Ô' => 'O', 'Ố' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O', 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', ' ' => '-', ',' => '', '.' => '', '&' => '', '^' => '', '!' => '', '@' => '', '#' => '', '$' => '', '%' => '', '*' => '', '~' => '', '`' => '');
        return strtolower(strtr($string, $vieUnicode));
    }
    
    public function addStylesheet($href)
    {
        if(is_array($href)){
            foreach($href as $src){
                $this->styleSheet .= '<link href="'. $src .'" rel="stylesheet" />';
            }
        }else{
            $this->styleSheet .= '<link href="'. $href .'" rel="stylesheet" />';
        }
    }
    
    public function addHeadScript($src)
    {
        if(is_array($src)){
            foreach($src as $url){
                $this->headScript .= '<script src="'. $url .'"></script>';
            }
        }else{
            $this->headScript .= '<script src="'. $src .'"></script>';
        }
    }

}